package bd

import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProviders
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import oa.lla.cca.R
import com.clean.scanlibrary.bean.DiscernTokenBean
import bb.CD
import bb.CD.Companion.VIEW_FROM_PIC_STYLE
import bb.CL
import com.clean.scanlibrary.camera.utils.FileUtil
import oa.lla.cca.databinding.ActivityPicStyleLayoutBinding
import com.clean.scanlibrary.dialog.LoadingDialog
import com.clean.scanlibrary.http.DiscernViewMode
import com.clean.scanlibrary.http.HttpApi
import org.w3c.dom.Text

class CN:AppCompatActivity() {

    private var bind:ActivityPicStyleLayoutBinding? = null
    private var path = ""
    private val dataBind get() = bind!!
    private var loadingDialog: LoadingDialog? = null
    private var viewMode: DiscernViewMode? = null
    private var discernTokenBean : DiscernTokenBean? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bind = ActivityPicStyleLayoutBinding.inflate(layoutInflater)
        setContentView(bind!!.root)
        initView()
    }

    companion object{
       fun intentPicStyleActivity(context: Context,path:String){
           val intent:Intent = Intent(context,CN::class.java)
           intent.putExtra("locationPath",path)
           context.startActivity(intent)
       }
    }


    private fun initView() {

        loadingDialog =
            LoadingDialog(this, R.style.dialog)
        loadingDialog?.show()
        viewMode = ViewModelProviders.of(this).get(DiscernViewMode::class.java)
        viewMode?.getDiscernToken(HttpApi.discernPicsAPIKey, HttpApi.discernPicSecretKey)

        viewMode?.getTokenLiveData?.observe(this
        ) { t ->
            loadingDialog?.dismiss()
            discernTokenBean = t
            Log.i("HttpManager==","discernTokenBean=="+discernTokenBean?.access_token)
        }

        viewMode?.getStylePicLiveData?.observe(this) { t ->
            loadingDialog?.dismiss()
            if (t != null && !TextUtils.isEmpty(t.image)) {
                val input: ByteArray = android.util.Base64.decode(t.image, android.util.Base64.DEFAULT)
                val bitmap = BitmapFactory.decodeByteArray(input, 0, input!!.size)
                Glide.with(this).load(bitmap).into(dataBind.topPic)
            } else {
                Toast.makeText(this, "转化失败", Toast.LENGTH_SHORT).show()
            }
        }


         path = intent.getStringExtra("locationPath").toString();


        Log.i("HttpManager==","path==="+path)
         dataBind.backLeft.setOnClickListener { finish() }
         Glide.with(this).load(path).into(dataBind.topPic)
        val compressQuality:String = FileUtil.compressQuality(this,path)

        dataBind.style1.setOnClickListener {

            Log.i("HttpManager==","access_token=="+discernTokenBean?.access_token)
            if(TextUtils.isEmpty(discernTokenBean?.access_token) || TextUtils.isEmpty(path)){
                ToastUtils.showShort("识别异常，请退出稍后重试。")
            }else{
                loadingDialog?.show()

                viewMode!!.getStylesTransPic(discernTokenBean!!.access_token,compressQuality,VIEW_FROM_PIC_STYLE,"cartoon")
            }
        }

        dataBind.style2.setOnClickListener {
            if(TextUtils.isEmpty(discernTokenBean?.access_token) || TextUtils.isEmpty(path)){
                ToastUtils.showShort("识别异常，请退出稍后重试。")
            }else{
                loadingDialog?.show()
                viewMode!!.getStylesTransPic(discernTokenBean!!.access_token,compressQuality,VIEW_FROM_PIC_STYLE,"pencil")
            }
        }


        dataBind.style3.setOnClickListener {
            if(TextUtils.isEmpty(discernTokenBean?.access_token) || TextUtils.isEmpty(path)){
                ToastUtils.showShort("识别异常，请退出稍后重试。")
            }else{
                loadingDialog?.show()
                viewMode!!.getStylesTransPic(discernTokenBean!!.access_token,compressQuality,VIEW_FROM_PIC_STYLE,"color_pencil")
            }
        }


        dataBind.style4.setOnClickListener {
            if(TextUtils.isEmpty(discernTokenBean?.access_token) || TextUtils.isEmpty(path)){
                ToastUtils.showShort("识别异常，请退出稍后重试。")
            }else{
                loadingDialog?.show()
                viewMode!!.getStylesTransPic(discernTokenBean!!.access_token,compressQuality,VIEW_FROM_PIC_STYLE,"warm")
            }
        }


    }
}